﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using AstekServis.Views.Admin;
using AstekServis.Web.Data;
using System.ServiceModel.DomainServices.Client;
using DevExpress.AgDataGrid;
using DevExpress.AgMenu;
using System.Collections.Generic;
using System.Linq;
using AstekServis;

namespace AstekServis.Views
{
    public partial class Home : Page
    {
        private enum IslemListesi
        {
            AnaSayfa,
            MontajListesi,
            ServisListesi,
            MontajYapilacaklar,
            AracListesi
        }



        private void MontajYapilacaklarListesi()
        {
            AsTekDomainContext context = new AsTekDomainContext();
            EntityQuery<YeniMontaj> qry = context.GetYeniMontajListesiQuery().OrderByDescending(x => x.Tarih);
            LoadOperation<YeniMontaj> operation = context.Load(qry);
            GridControl1.DataSource = null;
            GridControl1.Columns.Clear();
            operation.Completed += (ss, oo) =>
            {
                LoadOperation<YeniMontaj> op = ss as LoadOperation<YeniMontaj>;
                if (op.HasError)
                {
                    System.Windows.MessageBox.Show(op.Error.ToString(), "Load Error",
                    System.Windows.MessageBoxButton.OK);
                    op.MarkErrorAsHandled();
                }
                else
                {
                    GridControl1.Columns.Clear();
                    GridControl1.AddColumn("Tarih");
                    GridControl1.AddColumn("FirmaKodu");
                    GridControl1.AddColumn("FirmaUnvani");
                    GridControl1.AddColumn("GrupKodu");
                    GridControl1.AddColumn("GrupAdi");
                    GridControl1.AddColumn("Seri");
                    GridControl1.AddColumn("Sira");
                    GridControl1.AddColumn("BelgeNo");

                    //GridControl1.AddColumn("FaturaTutari");
                    GridControl1.DataSource = op.Entities;

                    GridControl1.FilterString = "";
                    GridControl1.ShowTotals = true;
                    GridControl1.TotalSummary.Clear();
                    GridControl1.TotalSummary.Add(new AgDataGridSummaryItem(DevExpress.Data.SummaryItemType.Count, "Tarih", "") { Title = "Toplam :" });
                }
            };
        }
        private void MontajListesi(bool acikMontaj)
        {
            AsTekDomainContext context = new AsTekDomainContext();
            EntityQuery<MontajListe> qry;
            if (acikMontaj)
            {
                qry = context.GetMontajListesiQuery().Where(x => x.Durum == 0).OrderByDescending(x => x.Tarih);
            }
            else
            {
                qry = context.GetMontajListesiQuery().OrderByDescending(x => x.Tarih);
            }
            LoadOperation<MontajListe> operation = context.Load(qry);
            GridControl1.DataSource = null;
            GridControl1.Columns.Clear();
            operation.Completed += (ss, oo) =>
            {
                LoadOperation<MontajListe> op = ss as LoadOperation<MontajListe>;
                if (op.HasError)
                {
                    System.Windows.MessageBox.Show(op.Error.ToString(), "Load Error",
                    System.Windows.MessageBoxButton.OK);
                    op.MarkErrorAsHandled();
                }
                else
                {
                    GridControl1.AddColumn("Tarih");
                    GridControl1.AddColumn("Plaka");
                    GridControl1.AddColumn("Firma");
                    GridControl1.AddColumn("Grup");
                    GridControl1.AddColumn("BelgeNo");

                    GridControl1.DataSource = op.Entities;

                    GridControl1.FilterString = "";
                    GridControl1.ShowTotals = true;
                    GridControl1.TotalSummary.Clear();
                    GridControl1.TotalSummary.Add(new AgDataGridSummaryItem(DevExpress.Data.SummaryItemType.Count, "Tarih", "") { Title = "Toplam :" });
                }
            };
        }
        private void ServisListesi(bool acikServis)
        {
            AsTekDomainContext context2 = new AsTekDomainContext();
            EntityQuery<ServisListe> qry;
            if (acikServis)
            {
                qry = context2.GetServisListesiQuery().Where(x => x.Durum == "Açık").OrderByDescending(x => x.ServiseGidilecekTarih);
            }
            else
            {
                qry = context2.GetServisListesiQuery().OrderByDescending(x => x.ServiseGidilecekTarih);
            }
            LoadOperation<ServisListe> operation = context2.Load(qry);
            GridControl2.DataSource = null;
            GridControl2.Columns.Clear();

            operation.Completed += (ss, oo) =>
            {
                LoadOperation<ServisListe> op = ss as LoadOperation<ServisListe>;
                if (op.HasError)
                {
                    System.Windows.MessageBox.Show(op.Error.ToString(), "Load Error",
                    System.Windows.MessageBoxButton.OK);
                    op.MarkErrorAsHandled();
                }
                else
                {
                    GridControl2.AddColumn("ServiseGidilecekTarih");
                    GridControl2.AddColumn("Plaka");
                    GridControl2.AddColumn("Firma");
                    GridControl2.AddColumn("Grup");
                    GridControl2.AddColumn("Teknisyenler");
                    GridControl2.AddColumn("Servis");
                    GridControl2.AddColumn("Sorunlar");
                    GridControl2.AddColumn("Durum");
                    GridControl2.AddColumn("KapamaNedeni");
                    GridControl2.DataSource = op.Entities;

                    GridControl2.FilterString = "";
                    GridControl2.ShowTotals = true;
                    GridControl2.TotalSummary.Clear();
                    GridControl2.TotalSummary.Add(new AgDataGridSummaryItem(DevExpress.Data.SummaryItemType.Count, "ServiseGidilecekTarih", "") { Title = "Toplam :" });
                }
            };
        }
        private void AracListesi()
        {
            AsTekDomainContext context = new AsTekDomainContext();
            EntityQuery<AracBilgi> qry;
            qry = context.GetAracBilgileriQuery();
            LoadOperation<AracBilgi> operation = context.Load(qry);

            GridControl2.DataSource = null;
            GridControl2.Columns.Clear();

            operation.Completed += (ss, oo) =>
            {
                LoadOperation<AracBilgi> op = ss as LoadOperation<AracBilgi>;
                if (op.HasError)
                {
                    System.Windows.MessageBox.Show(op.Error.ToString(), "Load Error",
                    System.Windows.MessageBoxButton.OK);
                    op.MarkErrorAsHandled();
                }
                else
                {
                    GridControl2.AddColumn("Plaka");
                    GridControl2.AddColumn("FirmaAdi");
                    GridControl2.AddColumn("GrupAdi");
                    GridControl2.AddColumn("Adres");
                    GridControl2.AddColumn("AracKodu");
                    GridControl2.DataSource = op.Entities;

                    GridControl2.FilterString = "";
                    GridControl2.ShowTotals = true;
                    GridControl2.TotalSummary.Clear();
                    GridControl2.TotalSummary.Add(new AgDataGridSummaryItem(DevExpress.Data.SummaryItemType.Count, "Plaka", "") { Title = "Toplam :" });
                }
            };
        }
        private void Listele(IslemListesi islem)
        {
            switch (islem)
            {
                case IslemListesi.AnaSayfa:
                    lblHeader2.Text = "Açık Servisler";
                    lblHeader1.Text = "Açık Montajlar";

                    lblHeader2.Visibility = System.Windows.Visibility.Visible;
                    border2.Visibility = System.Windows.Visibility.Visible;
                    GridControl2.Visibility = System.Windows.Visibility.Visible;

                    lblHeader1.Visibility = System.Windows.Visibility.Visible;
                    border1.Visibility = System.Windows.Visibility.Visible;
                    GridControl1.Visibility = System.Windows.Visibility.Visible;

                    GridControl1.Height = 200;
                    GridControl2.Height = 200;

                    MontajListesi(true);
                    ServisListesi(true);

                    anaSayfaMenuCreate();
                    break;
                case IslemListesi.MontajListesi:
                    lblHeader1.Text = "Montaj Listesi";
                    lblHeader1.Visibility = System.Windows.Visibility.Visible;
                    border1.Visibility = System.Windows.Visibility.Visible;
                    GridControl1.Visibility = System.Windows.Visibility.Visible;
                    lblHeader2.Visibility = System.Windows.Visibility.Collapsed;
                    border2.Visibility = System.Windows.Visibility.Collapsed;
                    GridControl2.Visibility = System.Windows.Visibility.Collapsed;
                    GridControl1.Height = 400;
                    MontajListesi(false);
                    montajGorClick();
                    break;
                case IslemListesi.ServisListesi:
                    lblHeader1.Visibility = System.Windows.Visibility.Collapsed;
                    border1.Visibility = System.Windows.Visibility.Collapsed;
                    GridControl1.Visibility = System.Windows.Visibility.Collapsed;
                    lblHeader2.Text = "Servis Listesi";
                    lblHeader2.Visibility = System.Windows.Visibility.Visible;
                    border2.Visibility = System.Windows.Visibility.Visible;
                    GridControl2.Visibility = System.Windows.Visibility.Visible;
                    GridControl2.Height = 400;
                    ServisListesi(false);
                    ServisMenuClick();
                    break;
                case IslemListesi.MontajYapilacaklar:
                    lblHeader2.Visibility = System.Windows.Visibility.Collapsed;
                    border2.Visibility = System.Windows.Visibility.Collapsed;
                    GridControl2.Visibility = System.Windows.Visibility.Collapsed;
                    lblHeader1.Text = "Montaj yapılacaklar listesi";
                    lblHeader1.Visibility = System.Windows.Visibility.Visible;
                    border1.Visibility = System.Windows.Visibility.Visible;
                    GridControl1.Visibility = System.Windows.Visibility.Visible;
                    GridControl1.Height = 400;
                    MontajYapilacaklarListesi();
                    yeniMontajMenuClick();
                    break;
                case IslemListesi.AracListesi:
                    lblHeader1.Visibility = System.Windows.Visibility.Collapsed;
                    border1.Visibility = System.Windows.Visibility.Collapsed;
                    GridControl1.Visibility = System.Windows.Visibility.Collapsed;
                    lblHeader2.Text = "Araç Listesi";
                    lblHeader2.Visibility = System.Windows.Visibility.Visible;
                    border2.Visibility = System.Windows.Visibility.Visible;
                    GridControl2.Visibility = System.Windows.Visibility.Visible;
                    GridControl2.Height = 400;
                    AracListesi();
                    aracBilgiMenuClick();
                    break;
                default:
                    break;
            }
        }
        private void LoadRaporlar()
        {
            AsTekDomainContext context = new AsTekDomainContext();
            EntityQuery<Rapor> qry = context.GetRaporlarQuery();
            LoadOperation<Rapor> operation = context.Load(qry);
            operation.Completed += (ss, oo) =>
            {
                LoadOperation<Rapor> op = ss as LoadOperation<Rapor>;
                if (op.HasError)
                {
                    System.Windows.MessageBox.Show(op.Error.ToString(), "Load Error",
                    System.Windows.MessageBoxButton.OK);
                    op.MarkErrorAsHandled();
                }
                else
                {
                    var liste = op.Entities.ToList();
                    foreach (var item in liste)
                    {
                        MenuItem mn = new MenuItem();
                        mn.Header = item.Adi;
                        mn.Tag = item;
                        mn.Click += new RoutedEventHandler(mn_Click);
                        mnRapor.Items.Add(mn);
                    }
                }
            };
        }

        void mn_Click(object sender, RoutedEventArgs e)
        {
            MenuItem mn = (MenuItem)sender;
            Rapor rp = (Rapor)mn.Tag;
            if (rp.GridSorgu.ToLower().IndexOf("where") > -1)
            {
                RaporParametre prm = new RaporParametre();
                prm.Rapor = new Rapor() { Adi = rp.Adi, ChartAlan = rp.ChartAlan, ChartDeger = rp.ChartDeger, ChartSorgu = rp.ChartSorgu, ChartTipi = rp.ChartTipi, GridSorgu = rp.GridSorgu, ID = rp.ID, Tipi = rp.Tipi };
                prm.Show();
                prm.Closed += prm_Closed;
            }
            else
            {
                RaporCalistir(rp);
            }
        }

        void prm_Closed(object sender, EventArgs e)
        {
            RaporParametre lw = (RaporParametre)sender;
            if (lw.DialogResult == true)
            {
                RaporCalistir(lw.Rapor);
            }
        }

        private void ModemDurumlari()
        {
            AsTekDomainContext context = new AsTekDomainContext();
            context.ChangeWcfSendTimeout(new TimeSpan(0, 10, 0));

            InvokeOperation<ModemDurumu[]> operation = context.ModemDurumlari();
            operation.Completed += (ss, oo) =>
            {
                RaporView rpr = new RaporView();
                InvokeOperation<ModemDurumu[]> op = ss as InvokeOperation<ModemDurumu[]>;
                if (op.HasError)
                {
                    System.Windows.MessageBox.Show(op.Error.ToString(), "Load Error",
                    System.Windows.MessageBoxButton.OK);
                    op.MarkErrorAsHandled();
                }
                else
                {
                    rpr.Title = "Modem Durumları";
                    rpr.RaporTipi = 3;
                    rpr.ModemData = op.Value;
                    rpr.Show();
                }
            };

        }

        private void RaporCalistir(Rapor rp)
        {
            string sql1 = (rp.ChartSorgu == "0") ? "" : rp.ChartSorgu;

            AsTekDomainContext context = new AsTekDomainContext();
            InvokeOperation<string[]> operation = context.GetSqlQuery(rp.GridSorgu, sql1);
            operation.Completed += (ss, oo) =>
            {
                RaporView rpr = new RaporView();
                InvokeOperation<string[]> op = ss as InvokeOperation<string[]>;
                if (op.HasError)
                {
                    System.Windows.MessageBox.Show(op.Error.ToString(), "Load Error",
                    System.Windows.MessageBoxButton.OK);
                    op.MarkErrorAsHandled();
                }
                else
                {
                    rpr.Title = rp.Adi;
                    rpr.GridData = op.Value[0];
                    rpr.GrafikData = op.Value[1];
                    rpr.GrafikTipi = rp.ChartTipi;
                    rpr.GrafikAlan = rp.ChartAlan;
                    rpr.GrafikDeger = rp.ChartDeger;
                    rpr.RaporTipi = rp.Tipi;
                    rpr.Show();
                }
            };

        }

        private void yeniMontajMenuClick()
        {
            AgMenuItem mn = new AgMenuItem();
            mn.Click += (ss, oo) =>
            {
                AracTanitim frm = new AracTanitim();
                frm.YeniArac = true;
                object row = GridControl1.FocusedDataRow;
                if (row != null)
                {
                    YeniMontaj yeni = (YeniMontaj)row;
                    frm.MontajBilgi = new MontajBilgi()
                    {
                        Adres = yeni.Adres,
                        BelgeNo = yeni.BelgeNo + "  /  " + yeni.Seri + "-" + yeni.Sira.ToString(),
                        FirmaAdi = yeni.FirmaUnvani,
                        GrupAdi = yeni.GrupAdi,
                        Tarih = yeni.Tarih.Value,
                        ID = -1,
                        AltCariKodu = yeni.FirmaKodu,
                        AnaCariKodu = yeni.GrupKodu,
                        AracKodu = yeni.AracKodu,
                        FaturaID = yeni.ID,
                        DbNo = yeni.DbNo
                    };
                }
                frm.Show();
            };
            mn.Header = "Yeni Montaj";
            agPopupMenu1.Items.Clear();
            agPopupMenu1.Items.Add(mn);

            AgMenuItem exp = new AgMenuItem();
            exp.Click += (ss, oo) =>
            {
                GridControl1.Export<YeniMontaj>();
            };
            exp.Header = "Dışarı Aktar";
            agPopupMenu1.Items.Add(exp);
        }
        private void montajGorClick()
        {
            AgMenuItem mn = new AgMenuItem();
            mn.Click += (ss, oo) =>
            {
                AracTanitim frm = new AracTanitim();
                frm.YeniArac = false;
                object row = GridControl1.FocusedDataRow;
                if (row != null)
                {
                    MontajListe yeni = (MontajListe)row;
                    frm.MontajBilgi = new MontajBilgi()
                    {
                        Adres = yeni.Adres,
                        BelgeNo = yeni.BelgeNo,
                        FirmaAdi = yeni.Firma,
                        GrupAdi = yeni.Grup,
                        Tarih = yeni.Tarih,
                        ID = yeni.ID,
                        AltCariKodu = yeni.AltCariKodu,
                        AnaCariKodu = yeni.AnaCariKodu,
                        AracKodu = yeni.AracKodu,
                        FaturaID = yeni.FaturaID
                    };

                    Kullanici kullanici = Session.Instance.GetValue<Kullanici>("user");
                    if (kullanici.YetkiSeviyesi == 0)
                    {
                        frm.dteMontajTarihi.IsEnabled = false;
                    }
                    frm.Show();
                }
            };
            mn.Header = "Montaj Bilgisi";
            agPopupMenu1.Items.Clear();
            agPopupMenu1.Items.Add(mn);

            AgMenuItem mn2 = new AgMenuItem();
            mn2.Click += (ss, oo) =>
            {
                object row = GridControl1.FocusedDataRow;
                if (row != null)
                {
                    MontajListe yeni = (MontajListe)row;
                    AsTekDomainContext context = new AsTekDomainContext();
                    LoadOperation<AracHareketi> operation = context.Load(context.GetAracHareketleriQuery().Where(x => x.ID == yeni.ID));
                    operation.Completed += (s, o) =>
                    {
                        LoadOperation<AracHareketi> op = s as LoadOperation<AracHareketi>;
                        if (op.HasError)
                        {
                            System.Windows.MessageBox.Show(op.Error.ToString(), "Load Error",
                            System.Windows.MessageBoxButton.OK);
                            op.MarkErrorAsHandled();
                        }
                        else
                        {
                            AracHareketi hr = op.Entities.FirstOrDefault();
                            context.AracHareketis.Remove(hr);
                            context.SubmitChanges(sso =>
                            {
                                if (sso.HasError)
                                    MessageBox.Show("Hata:\r\n" + sso.Error.Message);
                                else
                                {
                                    MontajListesi(false);
                                }
                            }, null);
                        }
                    };
                }
            };
            mn2.Header = "Montajı Sil";
            agPopupMenu1.Items.Add(mn2);


            AgMenuItem exp = new AgMenuItem();
            exp.Click += (ss, oo) =>
            {
                GridControl1.Export<MontajListe>();
            };
            exp.Header = "Dışarı Aktar";
            agPopupMenu1.Items.Add(exp);

        }
        private void ServisMenuClick()
        {
            AgMenuItem mn = new AgMenuItem();
            mn.Click += (ss, oo) =>
            {
                object row = GridControl2.FocusedDataRow;
                if (row != null)
                {
                    yeniServis frm = new yeniServis();
                    ServisListe yeni = (ServisListe)row;
                    frm.ServisID = yeni.ID;
                    frm.Show();
                }
            };
            mn.Header = "Servis Bilgisi";
            agPopupMenu1.Items.Clear();
            agPopupMenu1.Items.Add(mn);

            AgMenuItem mn2 = new AgMenuItem();
            mn2.Click += (ss, oo) =>
            {
                object row = GridControl2.FocusedDataRow;
                if (row != null)
                {
                    Kapatma frm = new Kapatma();
                    frm.ServisYonlendirme = true;
                    ServisListe yeni = (ServisListe)row;
                    frm.HareketID = yeni.ID;
                    frm.Show();
                }
            };
            mn2.Header = "Servis'i Yönlendir";
            agPopupMenu1.Items.Add(mn2);


            AgMenuItem mn3 = new AgMenuItem();
            mn3.Click += (ss, oo) =>
            {
                object row = GridControl2.FocusedDataRow;
                if (row != null)
                {
                    ServisListe yeni = (ServisListe)row;
                    AsTekDomainContext context = new AsTekDomainContext();
                    LoadOperation<AracHareketi> operation = context.Load(context.GetAracHareketleriQuery().Where(x => x.ID == yeni.ID));
                    operation.Completed += (s, o) =>
                    {
                        LoadOperation<AracHareketi> op = s as LoadOperation<AracHareketi>;
                        if (op.HasError)
                        {
                            System.Windows.MessageBox.Show(op.Error.ToString(), "Load Error",
                            System.Windows.MessageBoxButton.OK);
                            op.MarkErrorAsHandled();
                        }
                        else
                        {
                            AracHareketi hr = op.Entities.FirstOrDefault();
                            context.AracHareketis.Remove(hr);
                            context.SubmitChanges(sso =>
                            {
                                if (sso.HasError)
                                    MessageBox.Show("Hata:\r\n" + sso.Error.Message);
                                else
                                {
                                    ServisListesi(false);
                                }
                            }, null);
                        }
                    };
                }
            };
            mn3.Header = "Servis'i Sil";
            agPopupMenu1.Items.Add(mn3);

            AgMenuItem exp = new AgMenuItem();
            exp.Click += (ss, oo) =>
            {
                GridControl2.Export<ServisListe>();
            };
            exp.Header = "Dışarı Aktar";
            agPopupMenu1.Items.Add(exp);
        }
        private void anaSayfaMenuCreate()
        {
            Kullanici kullanici = Session.Instance.GetValue<Kullanici>("user");
            agPopupMenu1.Height = 200;
            agPopupMenu1.Items.Clear();

            AgMenuItem mn = new AgMenuItem();
            mn.Click += (ss, oo) =>
            {
                AracTanitim frm = new AracTanitim();
                frm.YeniArac = false;
                object row = GridControl1.FocusedDataRow;
                if (row != null)
                {
                    MontajListe yeni = (MontajListe)row;
                    frm.MontajBilgi = new MontajBilgi()
                    {
                        Adres = yeni.Adres,
                        BelgeNo = yeni.BelgeNo,
                        FirmaAdi = yeni.Firma,
                        GrupAdi = yeni.Grup,
                        Tarih = yeni.Tarih,
                        ID = yeni.ID,
                        AltCariKodu = yeni.AltCariKodu,
                        AnaCariKodu = yeni.AnaCariKodu,
                        AracKodu = yeni.AracKodu,
                        FaturaID = yeni.FaturaID
                    };

                    if (kullanici.YetkiSeviyesi == 0)
                    {
                        frm.dteMontajTarihi.IsEnabled = false;
                    }
                    frm.Show();
                }
            };
            mn.Header = "Montaj Bilgisi";
            agPopupMenu1.Items.Add(mn);


            AgMenuItem mn2 = new AgMenuItem();
            mn2.Click += (ss, oo) =>
            {
                object row = GridControl2.FocusedDataRow;
                if (row != null)
                {
                    yeniServis frm = new yeniServis();
                    ServisListe yeni = (ServisListe)row;
                    frm.ServisID = yeni.ID;
                    frm.Show();
                }
            };
            mn2.Header = "Servis Bilgisi";
            agPopupMenu1.Items.Add(mn2);



            if (kullanici.ServisKapatir)
            {
                AgMenuItem mn3 = new AgMenuItem();
                mn3.Click += (ss, oo) =>
                {
                    Kapatma frm = new Kapatma();
                    object row = GridControl2.FocusedDataRow;
                    if (row != null)
                    {
                        ServisListe yeni = (ServisListe)row;
                        frm.HareketID = yeni.ID;
                    }
                    frm.Show();
                };
                mn3.Header = "Servis'i Kapat";
                agPopupMenu1.Items.Add(mn3);
            }


            if (kullanici.MontajKapatir)
            {
                AgMenuItem mn4 = new AgMenuItem();
                mn4.Click += (ss, oo) =>
                {
                    Kapatma frm = new Kapatma();
                    object row = GridControl1.FocusedDataRow;
                    if (row != null)
                    {
                        MontajListe yeni = (MontajListe)row;
                        frm.HareketID = yeni.ID;
                    }
                    frm.Show();
                };
                mn4.Header = "Montaj'ı Kapat";
                agPopupMenu1.Items.Add(mn4);
            }
            


            AgMenuItem mnsil = new AgMenuItem();
            mnsil.Click += (ss, oo) =>
            {
                object row = GridControl1.FocusedDataRow;
                if (row != null)
                {
                    MontajListe yeni = (MontajListe)row;
                    AsTekDomainContext context = new AsTekDomainContext();
                    LoadOperation<AracHareketi> operation = context.Load(context.GetAracHareketleriQuery().Where(x => x.ID == yeni.ID));
                    operation.Completed += (s, o) =>
                    {
                        LoadOperation<AracHareketi> op = s as LoadOperation<AracHareketi>;
                        if (op.HasError)
                        {
                            System.Windows.MessageBox.Show(op.Error.ToString(), "Load Error",
                            System.Windows.MessageBoxButton.OK);
                            op.MarkErrorAsHandled();
                        }
                        else
                        {
                            AracHareketi hr = op.Entities.FirstOrDefault();
                            context.AracHareketis.Remove(hr);
                            context.SubmitChanges(sso =>
                            {
                                if (sso.HasError)
                                    MessageBox.Show("Hata:\r\n" + sso.Error.Message);
                                else
                                {
                                    MontajListesi(true);
                                }
                            }, null);
                        }
                    };
                }
            };
            mnsil.Header = "Montajı Sil";
            agPopupMenu1.Items.Add(mnsil);

            AgMenuItem mnsil2 = new AgMenuItem();
            mnsil2.Click += (ss, oo) =>
            {
                object row = GridControl2.FocusedDataRow;
                if (row != null)
                {
                    ServisListe yeni = (ServisListe)row;
                    AsTekDomainContext context = new AsTekDomainContext();
                    LoadOperation<AracHareketi> operation = context.Load(context.GetAracHareketleriQuery().Where(x => x.ID == yeni.ID));
                    operation.Completed += (s, o) =>
                    {
                        LoadOperation<AracHareketi> op = s as LoadOperation<AracHareketi>;
                        if (op.HasError)
                        {
                            System.Windows.MessageBox.Show(op.Error.ToString(), "Load Error",
                            System.Windows.MessageBoxButton.OK);
                            op.MarkErrorAsHandled();
                        }
                        else
                        {
                            AracHareketi hr = op.Entities.FirstOrDefault();
                            context.AracHareketis.Remove(hr);
                            context.SubmitChanges(sso =>
                            {
                                if (sso.HasError)
                                    MessageBox.Show("Hata:\r\n" + sso.Error.Message);
                                else
                                {
                                    ServisListesi(true);
                                }
                            }, null);
                        }
                    };
                }
            };
            mnsil2.Header = "Servis'i Sil";
            agPopupMenu1.Items.Add(mnsil2);


            AgMenuItem exp = new AgMenuItem();
            exp.Click += (ss, oo) =>
            {
                GridControl1.Export<MontajListe>();
            };
            exp.Header = "Montajları Dışarı Aktar";
            agPopupMenu1.Items.Add(exp);

            AgMenuItem exp2 = new AgMenuItem();
            exp2.Click += (ss, oo) =>
            {
                GridControl2.Export<ServisListe>();
            };
            exp2.Header = "Servisleri Dışarı Aktar";
            agPopupMenu1.Items.Add(exp2);

        }
        private void aracBilgiMenuClick()
        {
            AgMenuItem mn = new AgMenuItem();
            mn.Click += (ss, oo) =>
            {
                BilgiEkrani frm = new BilgiEkrani();
                object row = GridControl2.FocusedDataRow;
                if (row != null)
                {
                    AracBilgi yeni = (AracBilgi)row;
                    frm.AracKodu = yeni.AracKodu;
                    frm.FirmaAdi = yeni.FirmaAdi;
                    frm.GrupAdi = yeni.GrupAdi;
                    frm.Adres = yeni.Adres;
                }
                frm.Show();
            };
            mn.Header = "Araç Bilgileri";
            agPopupMenu1.Items.Clear();
            agPopupMenu1.Items.Add(mn);

            AgMenuItem mnsil = new AgMenuItem();
            mnsil.Click += (ss, oo) =>
            {
                object row = GridControl2.FocusedDataRow;
                if (row != null)
                {
                    AracBilgi yeni = (AracBilgi)row;
                    AsTekDomainContext context = new AsTekDomainContext();
                    LoadOperation<Arac> operation = context.Load(context.GetAraclarQuery().Where(x => x.ID == yeni.ID));
                    operation.Completed += (s, o) =>
                    {
                        LoadOperation<Arac> op = s as LoadOperation<Arac>;
                        if (op.HasError)
                        {
                            System.Windows.MessageBox.Show(op.Error.ToString(), "Load Error",
                            System.Windows.MessageBoxButton.OK);
                            op.MarkErrorAsHandled();
                        }
                        else
                        {
                            Arac hr = op.Entities.FirstOrDefault();
                            context.Aracs.Remove(hr);
                            context.SubmitChanges(sso =>
                            {
                                if (sso.HasError)
                                    MessageBox.Show("Hata:\r\n" + sso.Error.Message);
                                else
                                {
                                    AracListesi();
                                }
                            }, null);
                        }
                    };
                }
            };
            mnsil.Header = "Araç Sil";
            agPopupMenu1.Items.Add(mnsil);


            AgMenuItem exp = new AgMenuItem();
            exp.Click += (ss, oo) =>
            {
                GridControl2.Export<AracBilgi>();
            };
            exp.Header = "Dışarı Aktar";
            agPopupMenu1.Items.Add(exp);
        }
        #region Events

        public Home()
        {
            InitializeComponent();
            this.Loaded += new System.Windows.RoutedEventHandler(Home_Loaded);
            this.MouseRightButtonDown += new System.Windows.Input.MouseButtonEventHandler(Home_MouseRightButtonDown);
        }
        void Home_MouseRightButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            e.Handled = true;
            if (e.OriginalSource is TextBlock)
            {
                if (agPopupMenu1.Items.Count > 0)
                {
                    agPopupMenu1.Popup((TextBlock)e.OriginalSource, GetButtonRectPosition((TextBlock)e.OriginalSource));
                }
            }
        }
        Rect GetButtonRectPosition(TextBlock btn)
        {
            Point leftTop = btn.TransformToVisual(Application.Current.RootVisual).Transform(new Point(0, 0));
            leftTop.Y++;
            Rect res = new Rect(leftTop, new Size(btn.ActualWidth, btn.ActualHeight));
            return res;
        }

        private void ModemDurumlari_Click(object sender, System.EventArgs e)
        {
            ModemDurumlari();
        }
        void Home_Loaded(object sender, System.Windows.RoutedEventArgs e)
        {
            MainPage m = (MainPage)Application.Current.RootVisual;
            Kullanici kullanici = Session.Instance.GetValue<Kullanici>("user");
            if (kullanici != null)
            {
                m.lnkLogin.Content = kullanici.TamAdi + " Çıkış";
                if (kullanici.YetkiSeviyesi == 0)
                    mnYonetim.Visibility = System.Windows.Visibility.Collapsed;

                LoadRaporlar();
                Listele(IslemListesi.AnaSayfa);
            }
            else
            {
                this.NavigationService.Navigate(new Uri("/Login", UriKind.Relative));
            }
        }
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
        }
        private void KullaniciTanitim_Click(object sender, System.EventArgs e)
        {
            KullaniciTanitim frm = new KullaniciTanitim();
            frm.Show();
        }
        private void AracListesi_Click(object sender, System.EventArgs e)
        {
            Listele(IslemListesi.AracListesi);
        }
        private void ServisMerkezi_Click(object sender, EventArgs e)
        {
            ServisMerkeziTanitim frm = new ServisMerkeziTanitim();
            frm.KartTipi = ServisMerkeziTanitim.KartTipleri.ServisMerkezi;
            frm.Show();
        }
        private void AracMarkalari_Click(object sender, EventArgs e)
        {
            ServisMerkeziTanitim frm = new ServisMerkeziTanitim();
            frm.KartTipi = ServisMerkeziTanitim.KartTipleri.AracMarkalari;
            frm.Show();
        }
        private void ServisSorunlari_Click(object sender, EventArgs e)
        {
            ServisMerkeziTanitim frm = new ServisMerkeziTanitim();
            frm.KartTipi = ServisMerkeziTanitim.KartTipleri.ServisSorunlari;
            frm.Show();
        }
        private void SistemTanimlari_Click(object sender, EventArgs e)
        {
            ServisMerkeziTanitim frm = new ServisMerkeziTanitim();
            frm.KartTipi = ServisMerkeziTanitim.KartTipleri.SistemTanimlari;
            frm.Show();
        }
        private void Firmware_Click(object sender, EventArgs e)
        {
            ServisMerkeziTanitim frm = new ServisMerkeziTanitim();
            frm.KartTipi = ServisMerkeziTanitim.KartTipleri.FirmwareTanimlari;
            frm.Show();
        }
        private void Teknisyen_Click(object sender, EventArgs e)
        {
            ServisMerkeziTanitim frm = new ServisMerkeziTanitim();
            frm.KartTipi = ServisMerkeziTanitim.KartTipleri.TeknisyenTanitim;
            frm.Show();
        }
        private void OzellikTanim_Click(object sender, EventArgs e)
        {
            ServisMerkeziTanitim frm = new ServisMerkeziTanitim();
            frm.KartTipi = ServisMerkeziTanitim.KartTipleri.AracOzellikleri;
            frm.Show();
        }
        private void KontrolTanim_Click(object sender, EventArgs e)
        {
            ServisMerkeziTanitim frm = new ServisMerkeziTanitim();
            frm.KartTipi = ServisMerkeziTanitim.KartTipleri.KontrolTanimlari;
            frm.Show();
        }
        private void FilmTanim_Click(object sender, EventArgs e)
        {
            ServisMerkeziTanitim frm = new ServisMerkeziTanitim();
            frm.KartTipi = ServisMerkeziTanitim.KartTipleri.FilmTanimlari;
            frm.Show();
        }
        private void KapamaSebep_Click(object sender, EventArgs e)
        {
            ServisMerkeziTanitim frm = new ServisMerkeziTanitim();
            frm.KartTipi = ServisMerkeziTanitim.KartTipleri.KapamaSebepleri;
            frm.Show();
        }
        private void ParcaPaket_Click(object sender, EventArgs e)
        {
            ParcaPaketTanitim frm = new ParcaPaketTanitim();
            frm.Show();
        }
        private void FilmSozlesmeleri_Click(object sender, EventArgs e)
        {
            SozlesmeTanitim frm = new SozlesmeTanitim();
            frm.FormTipi = SozlesmeTanitim.FormTipleri.FilmSozlesmesi;
            frm.Show();
        }
        private void Parametre_Click(object sender, EventArgs e)
        {
            ServisMerkeziTanitim srv = new ServisMerkeziTanitim();
            srv.KartTipi = ServisMerkeziTanitim.KartTipleri.Parametreler;
            srv.Show();
        }
        private void HizmetKarti_Click(object sender, EventArgs e)
        {
            ServisMerkeziTanitim srv = new ServisMerkeziTanitim();
            srv.KartTipi = ServisMerkeziTanitim.KartTipleri.HizmetKartlari;
            srv.Show();
        }
        private void RaporTasarla_Click(object sender, EventArgs e)
        {
            RaporYap frm = new RaporYap();
            frm.Show();
        }
        private void GarantiSozlesmeleri_Click(object sender, EventArgs e)
        {
            SozlesmeTanitim frm = new SozlesmeTanitim();
            frm.FormTipi = SozlesmeTanitim.FormTipleri.GarantiSozlesmesi;
            frm.Show();
        }
        private void MontajListesi_Click(object sender, EventArgs e)
        {
            Listele(IslemListesi.MontajListesi);
        }
        private void ServisListesi_Click(object sender, EventArgs e)
        {
            Listele(IslemListesi.ServisListesi);
        }
        private void YeniMontajListesi_Click(object sender, EventArgs e)
        {
            Listele(IslemListesi.MontajYapilacaklar);
        }
        private void YeniServis_Click(object sender, EventArgs e)
        {
            yeniServis servis = new yeniServis();
            servis.Show();
        }
        private void AnaSayfa_Click(object sender, EventArgs e)
        {
            Listele(IslemListesi.AnaSayfa);
        }
        #endregion

        private void AgMenuItem_Click(object sender, EventArgs e)
        {
            EvrakTasarim frm = new EvrakTasarim();
            frm.TasarimTipi = EvrakTasarim.TasarimTipleri.HizmetFaturasi;
            frm.Show();
        }
    }
}
